from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import logging
logging.basicConfig(level=logging.INFO,format='%(asctime)s %(levelname)s %(name)s %(message)s')

class BasePage():
    def __init__(self, driver):
        self.driver = driver

    # 打开网页
    def get_url(self, url):
        self.driver.get(url)
        sleep(2)
        self.driver.maximize_window()

    # 定位元素
    def find_ele(self, loc):
        ele = self.driver.find_element(*loc)

        logging.info(f'定位{loc}元素成功')
        return ele

    # 输入元素
    def input_ele(self, loc, value):
        ele = self.find_ele(loc)
        ele.send_keys(value)
        logging.info(f'在{loc}中输入{value}成功')

    # 按钮点击
    def ele_click(self, loc):
        ele = self.find_ele(loc)
        ele.click()
        logging.info(f'在{loc}成功点击')
    # 文本清除
    def ele_clear(self, loc):
        ele = self.find_ele(loc)
        ele.clear()


if __name__ == '__main__':
    url = r'http://192.168.126.131/upload/'
    # 登录定位
    username_loc = (By.ID, 'ls_username')
    password_loc = (By.ID, 'ls_password')
    submit_loc=(By.XPATH,'//form[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button')

    username = 't01'
    password = '123456'

    chrome_location = r'E:\nmon\light_Chrome\chrome-win\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_location

    driver = webdriver.Chrome(options=options)

    base_page = BasePage(driver)
    base_page.get_url(url)
    base_page.ele_clear(username_loc)
    base_page.input_ele(username_loc,username)
    sleep(3)
    base_page.ele_clear(password_loc)
    base_page.input_ele(password_loc,password)
    sleep(3)
    base_page.ele_click(submit_loc)
    sleep(3)

    driver.quit()

